﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Services;
using GestionPedago.Services.ES;
using System.Windows.Input;
using System.Windows;

namespace GestionPedago.ViewModel {
    /// <summary>
    /// Classe qui représente l'onglet des alertes
    /// </summary>
    public class AfficherAlertesVM : OngletVM {

        private string message;
        public string Message {
            get {
                return this.message;
            }
            set {
                this.message = value;
                RaisePropertyChanged("Message");
            }
        }

        private ICommand filtrerCommand;
        public ICommand FiltrerCommand {
            get {
                if(this.filtrerCommand == null) {
                    this.filtrerCommand = new RelayCommand(param => this.UpdateAlertes());
                }
                return this.filtrerCommand;
            }
        }

        private ICommand switcherAlerteCommand;
        public ICommand SwitcherAlerteCommand {
            get {
                if(this.switcherAlerteCommand == null) {
                    this.switcherAlerteCommand = new RelayCommand(param => this.SwitcherAlerte(param));
                }
                return this.switcherAlerteCommand;
            }
        }

        private void SwitcherAlerte(object param) {
            AlerteES alerte = (AlerteES)param;
            EnregistrerAlerteIn paramEntree = new EnregistrerAlerteIn();
            paramEntree.Alerte = alerte;
            MessageBoxResult result = MessageBox.Show("Une fois désactivée, cette alerte ne sera plus visible.", "Confirmer", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
            
            if(MessageBoxResult.Yes.Equals(result)) {
                
                try 
                {
                    FacadeServices.GetInstance().EnregistrerAlerte(paramEntree);
                    Parent.vmMainWindow.vmBarreInfos.Message = "Alerte correctement désactivée";
                }
                catch(Exception e)
                {
                    MessageBox.Show("Erreur lors de la désactivation de l'alerte :\n" + e.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            } else {
                alerte.Actif = !alerte.Actif;
                UpdateAlertes();
            }
        }
        /// <summary>
        /// Méthode de mise à jour de la liste des alertes
        /// </summary>
        public void UpdateAlertes() {
            this.ListeAlertes = GetListeAlertes();
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="parent">Le gestionnaire d'onglet</param>
        public AfficherAlertesVM(GestionnaireOngletsVM parent) {
            base.DisplayName = "Mes alertes";
            base.Parent = parent;
        }

        private List<AlerteES> listeAlertes;

        public List<AlerteES> ListeAlertes {
            get {
                if(this.listeAlertes == null) {
                    this.listeAlertes = GetListeAlertes();
                }
                return this.listeAlertes;
            }

            set {
                this.listeAlertes = value;
                RaisePropertyChanged("ListeAlertes");
            }
        }

        /// <summary>
        /// Méthode d'appel au service RechercherListeAlertes
        /// </summary>
        /// <returns>Retourne la liste des alertes récupérées par le service</returns>
        private List<AlerteES> GetListeAlertes() {
            RechercherListeAlertesIn paramEntree = new RechercherListeAlertesIn();
            paramEntree.DateRappel = DateTime.Today;
            paramEntree.Message = this.Message;
            return FacadeServices.GetInstance().RechercherListeAlertes(paramEntree).ListeAlertes;
        }
    }
}
